<?php
namespace app\admin\controller;
/**
 * #系统配置#
 */
class Setting extends Common{
	/**
	 * #系统配置#{系统配置查看}
	 * @return [type] [description]
	 */
	function index(){
		$list=model('setting')->readAll([],0);
		$this->assign('list',$list);
		return view();
	}
	/**
	 * #微手设置#{微手专属设置}
	 * @return [type] [description]
	 */
	function weishou(){
		$list=model('setting')->readAll([],0);
		$this->assign('list',$list);
		return view();
	}
	/**
	 * #密码修改#{用户密码修改}
	 * @return [type] [description]
	 */
	function user(){
		if(request()->isAjax()||request()->isPost()){
			$old=input('old_pwd/s');
			$new=input('new_pwd/s');
			$renew=input('re_new_pwd/s');
			if(empty($old)){
				$this->error('请输入旧密码');
			}
			if(empty($new)){
				$this->error('请输入新密码');
			}
			if($old===$new){
				$this->error('新密码不能和旧密码相同');
			}
			if($new!==$renew){
				$this->error('两次密码不一致');
			}
			$uid=$this->user['id'];
			$u=model('member')->read(['id'=>$uid],'login_pwd');
			if($u['login_pwd']!==encrypt_pwd($old)){
				$this->error('密码验证失败');
			}
			$e=model('member')->edit(['id'=>$uid,'login_pwd'=>encrypt_pwd($new)]);
			if($e){
				session(config('keys.admin_user'),null);
				$this->success('密码修改成功，请重新登录!',url('login'));
				die;
			}else{
				$this->error('系统繁忙，密码更改失败!');
			}
		}
		return view();
	}
	/**
	 * #更改配置#{配置项更新、保存}
	 * 配置项更新、保存
	 * @return [type] [description]
	 */
	function edit(){
		$data=input('data/a',[]);
		if(count($data)){
			$model=model($this->table);
			$addData=[];
			$editData=[];
			foreach($data as $key => $v) {
				if(!isset($v['field']))continue;
				if($v['type']=='app_uploads'){
					unset($v['value']);
				}
				if(isset($v['id'])){
					$editData[]=$v;
				}else{
					$addData[]=$v;
				}
			}
			if(count($addData)){
				$model->add($addData);
			}
			if(count($editData)){
				$model->edit($editData);
			}
			$this->exportSetting();
			$this->success('保存成功');
		}

	}
	/**
	 * App 上传
	 * @return [type] [description]
	 */
	function appUploads($id=''){
		$file=request()->file('file');
		if($file){
			$name=input('name/s','app');
			$info=$file->validate(['ext'=>'apk'])->rule(function()use($name){
				return $name;
			})->move('./');
			if($info){
				$ext=$info->getExtension();
				$rs=[
					'ext'=>'.'.$ext,
					'link'=>'/'.$info->getSaveName(),
					'src'=>request()->domain().'/'.$info->getSaveName(),
				];
				
				$row=[
					'id'=>$id,
					'value'=>$rs['src']
				];
				model($this->table)->edit($row);
				$this->exportSetting();
				$this->success('上传成功','',$rs);
				die;
			}else{
				$this->error($file->getError());
			}
		}
	}
	/**
	 * 生成配置文件(方便引用)
	 * @param  string  $type  类型 admin | member
	 * @param  boolean  $isChange  是否发生改变
	 * @return array
	 */
	private function exportSetting(){
		$list=model('setting')->readAll();
		$arr=[];
		foreach($list as $v) {
			$arr[$v['field']]=$v['value'];
		}
		if(!empty($arr)){
			$path=env('config_path').'setting.php';
			$arrStr="<?php\n//请前往后台系统设置处设置此处仅为一份克隆版本\nreturn ";
			$arrStr.=var_export($arr,true);
			$arrStr.=';';
			$rs=file_put_contents($path,$arrStr);
			if(false===$rs){
				return error();
			}
			return success();
		}
	}
}